package com.whfc.emp.xxl.job;

import com.whfc.XxlJobConfig;
import com.whfc.emp.service.ClassMeetingInspectionService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.stereotype.Component;

/**
 * @Description 班会签到巡检定时任务
 * @Author dzg
 * @Date 2025-11-14
 * @Version 1.0
 */
@Component
@ConditionalOnBean(XxlJobConfig.class)
public class ClassMeetingInspectionJob {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ClassMeetingInspectionService classMeetingInspectionService;

    /**
     * 班会签到巡检定时任务
     * 建议执行频率：每30分钟执行一次
     * 任务描述：巡检当天已开始或结束的班会，检查未签到人员并发送提醒消息给监护人员
     */
    @XxlJob("classMeetingInspection")
    public void classMeetingInspection() {
        try {
            XxlJobHelper.log("开始执行班会签到巡检定时任务");

            // 调用班会签到巡检服务
            classMeetingInspectionService.executeInspection();

            XxlJobHelper.log("班会签到巡检定时任务执行完成");

        } catch (Exception ex) {
            logger.error("班会签到巡检定时任务执行失败", ex);
            XxlJobHelper.log("班会签到巡检定时任务执行失败 error: {}", ex.getMessage());
            XxlJobHelper.handleFail("班会签到巡检定时任务执行失败 error:" + ex.getLocalizedMessage());
        }
    }

    /**
     * 班会签到巡检手动触发任务
     * 用于手动触发班会签到巡检
     */
    @XxlJob("classMeetingInspectionManual")
    public void classMeetingInspectionManual() {
        try {
            XxlJobHelper.log("开始执行班会签到巡检手动触发任务");

            // 获取任务参数（可选）
            String paramStr = XxlJobHelper.getJobParam();
            if (paramStr != null && !paramStr.isEmpty()) {
                XxlJobHelper.log("任务参数: {}", paramStr);
            }

            // 调用班会签到巡检服务
            classMeetingInspectionService.executeInspection();

            XxlJobHelper.log("班会签到巡检手动触发任务执行完成");

        } catch (Exception ex) {
            logger.error("班会签到巡检手动触发任务执行失败", ex);
            XxlJobHelper.log("班会签到巡检手动触发任务执行失败 error: {}", ex.getMessage());
            XxlJobHelper.handleFail("班会签到巡检手动触发任务执行失败 error:" + ex.getLocalizedMessage());
        }
    }
}